package com.athuiju.hr.controller;


import com.athuiju.commonutils.R;
import com.athuiju.hr.entity.Sector;
import com.athuiju.hr.entity.vo.SectorVo;
import com.athuiju.hr.service.SectorService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wzh
 * @since 2021-12-15
 */
@RestController
@Api(value="组织架构controller",tags={"组织架构操作接口"})
@RequestMapping("/hr/sector")
public class SectorController {
    @Autowired
    private SectorService sectorService;

    @ApiOperation(value = "所有三级部门列表")
    @GetMapping("list")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Sector.class),
    })
    public R list(){
        List<Sector> sectorList=sectorService.getThree();
        return R.ok().data("sectorList",sectorList);
    }



    @ApiOperation(value = "部门菜单列表")
    @GetMapping("sectories")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Sector.class),
    })
    public R getAll(){
        List<Sector> sectorLists=sectorService.queryThree();
        return R.ok().data("sectorLists",sectorLists).data("total",sectorLists.size());
    }


    @ApiParam(value = "根据id修改架构信息")
    @ApiOperation(value = "根据id修改架构信息")
    @PostMapping("updateSector")
    public R updateSector(@RequestParam(name = "sector")String sector,
                          @RequestParam(name = "id")String id,
                          @RequestParam(name = "sort") Integer sort) {
     Sector sector1 = sectorService.getById(id);
     sector1.setSector(sector);
     sector1.setSort(sort);
     boolean flag = sectorService.updateById(sector1);
        if (flag) {
            return R.ok().data("flag",true);
        }
            return R.ok().data("flag",false);
    }

    @ApiOperation(value = "根据id查询架构信息")
    @GetMapping("getSectorInfo/{id}")
    public R getSectorInfo(@ApiParam(name = "id",value = "部门id",required = true)
                                 @PathVariable String id){
        Sector sector=sectorService.getById(id);
        return  R.ok().data("sector",sector);
    }

    @ApiOperation(value = "查询组织")
    @GetMapping("getOneSec")
    public R getOneSec(){
       List<Sector> list =  sectorService.getOneSec();
       return R.ok().data("list",list);
    }



    @ApiOperation(value = "删除")
    @PostMapping("delSectorByIds")
    public R delSectorByIds(
            @RequestBody List<String> ids) {
        boolean flag = sectorService.myRemoveById(ids);
        if (flag) {
            return R.ok().data("flag",true);
        }
        return R.error().data("flag",false);
    }


    @ApiParam(value = "修改组织状态")
    @ApiOperation(value = "修改组织状态")
    @PostMapping("updateSectorByIds/{state}")
    public R updateSectorByIds(@RequestBody List<String>ids,
                               @PathVariable("state")Integer state) {
        boolean flag = sectorService.myUpdateSectorByIds(ids,state);
        if (flag) {
            return R.ok().data("flag",true);
        }
        return R.ok().data("flag",false);

    }


    @ApiOperation(value = "添加部门")
    @PostMapping("addSector")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = SectorVo.class),
    })
    public R addSector(
            @ApiParam(name = "hr", value = "人员对象", required = true)
            @RequestBody SectorVo sectorVo) {
        boolean flag = sectorService.myAdd(sectorVo);
        if (flag) {
            return R.ok().data("flag",true);
        } else {
            return R.ok().data("flag",false);
        }
    }


    @ApiOperation(value = "根据id查询部门信息")
    @GetMapping("getBranInfo/{parentId}")
    public R getBranInfo(@ApiParam(name = "parentId",value = "部门id",required = true)
                           @PathVariable String parentId){
        List<Sector> branLists=sectorService.getBranById(parentId);
        return  R.ok().data("branLists",branLists);
    }

    @ApiOperation(value = "根据id查询子部门信息")
    @GetMapping("getBranSonInfo/{parentId}")
    public R getBranSonInfo(@ApiParam(name = "parentId",value = "部门id",required = true)
                         @PathVariable String parentId){
        List<Sector> branSonLists=sectorService.getBranSonById(parentId);
        return  R.ok().data("branSonLists",branSonLists);
    }
}

